class Solution {
public:
    int minPathCost(vector<vector<int>>& grid, vector<vector<int>>& moveCost) {
        int m = grid.size(), n = grid[0].size();
        vector dp(m + 1, vector(n + 1, 250000));

        for(int i = 0; i < n; ++i)
            dp[0][i] = grid[0][i];

        for(int i = 1; i < m; ++i)
        {
            for(int j = 0; j < n; ++j)
            {
                for(int k = 0; k < n; ++k)
                {
                    int last = grid[i - 1][k];
                    dp[i][j] = min(dp[i][j], grid[i][j] + moveCost[last][j] + dp[i - 1][k]);
                }
            }
        }
        return ranges::min(dp[m - 1]);
    }
};

